package jUnitPlugin;

import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.notification.Failure;
import socketEnv.Client;
import socketEnv.MessageCross;
import  org.junit.runner.Result;
import util.Info;

import java.lang.reflect.Method;
import java.util.List;

import static socketEnv.Client.message;

/**
 * Created by hanyz on 2016/8/31.
 */
public class RunRequiredTest extends Thread{
    public void run(){
        if (message == null) return;
        String className = message.getClassName();
        String methodName = message.getMethodName();
        Class c = null;
        Method m = null;
        try {
            c = Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            m = c.getMethod(methodName.replace(className + ".",""));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Info.out("Run Test:" + c.getName() + ":" + m.getName());
        Result result = new JUnitCore().run(Request.method(c, m.getName()));
        //这里如果写成m.getName()会执行上一个method，不知道是不是线程的问题
        MessageCross message = new MessageCross(Client.message);
        message.setResult(result);
        Client.send(message);
        System.out.println("成功执行测试案例！");
    }

}
